


<!DOCTYPE html>
<html id="htmlId">
<head>
  <title>Coverage Report > SpriteStore</title>
  <style type="text/css">
    @import "../../css/coverage.css";
    @import "../../css/highlight-idea.css";
  </style>
  <script type="text/javascript" src="../../js/highlight.pack.js"></script>
</head>

<body>
<div class="content">
<div class="breadCrumbs">
Current scope:     <a href="../../index.html">all classes</a>
    <span class="separator">|</span>
    <a href="../index.html">nl.tudelft.jpacman.sprite</a>
</div>

<h1>Coverage Summary for Class: SpriteStore (nl.tudelft.jpacman.sprite)</h1>

<table class="coverageStats">
<tr>
  <th class="name">Class</th>
<th class="coverageStat 
">
  Class, %
</th>
<th class="coverageStat 
">
  Method, %
</th>
<th class="coverageStat 
">
  Line, %
</th>
</tr>
<tr>
  <td class="name">SpriteStore</td>
<td class="coverageStat">
  <span class="percent">
    0%
  </span>
  <span class="absValue">
    (0/1)
  </span>
</td>
<td class="coverageStat">
  <span class="percent">
    0%
  </span>
  <span class="absValue">
    (0/5)
  </span>
</td>
<td class="coverageStat">
  <span class="percent">
    0%
  </span>
  <span class="absValue">
    (0/23)
  </span>
</td>
</tr>

</table>

<br/>
<br/>


<pre>
<div class="sourceCode" id="sourceCode"><i class="no-highlight">1</i>&nbsp;package nl.tudelft.jpacman.sprite;
<i class="no-highlight">2</i>&nbsp;
<i class="no-highlight">3</i>&nbsp;import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
<i class="no-highlight">4</i>&nbsp;
<i class="no-highlight">5</i>&nbsp;import java.awt.image.BufferedImage;
<i class="no-highlight">6</i>&nbsp;import java.io.IOException;
<i class="no-highlight">7</i>&nbsp;import java.io.InputStream;
<i class="no-highlight">8</i>&nbsp;import java.util.HashMap;
<i class="no-highlight">9</i>&nbsp;import java.util.Map;
<i class="no-highlight">10</i>&nbsp;
<i class="no-highlight">11</i>&nbsp;import javax.imageio.ImageIO;
<i class="no-highlight">12</i>&nbsp;
<i class="no-highlight">13</i>&nbsp;/**
<i class="no-highlight">14</i>&nbsp; * Utility to load {@link Sprite}s.
<i class="no-highlight">15</i>&nbsp; *
<i class="no-highlight">16</i>&nbsp; * @author Jeroen Roosen 
<i class="no-highlight">17</i>&nbsp; */
<b class="nc"><i class="no-highlight">18</i>&nbsp;public class SpriteStore {</b>
<i class="no-highlight">19</i>&nbsp;
<i class="no-highlight">20</i>&nbsp;    /**
<i class="no-highlight">21</i>&nbsp;     * We only need to load images once, so we keep track
<i class="no-highlight">22</i>&nbsp;     * of them in a hash map.
<i class="no-highlight">23</i>&nbsp;     */
<i class="no-highlight">24</i>&nbsp;    private final Map&lt;String, Sprite&gt; spriteMap;
<i class="no-highlight">25</i>&nbsp;
<i class="no-highlight">26</i>&nbsp;    /**
<i class="no-highlight">27</i>&nbsp;     * Create a new sprite store.
<i class="no-highlight">28</i>&nbsp;     */
<b class="nc"><i class="no-highlight">29</i>&nbsp;    public SpriteStore() {</b>
<b class="nc"><i class="no-highlight">30</i>&nbsp;        spriteMap = new HashMap&lt;&gt;();</b>
<b class="nc"><i class="no-highlight">31</i>&nbsp;    }</b>
<i class="no-highlight">32</i>&nbsp;
<i class="no-highlight">33</i>&nbsp;    /**
<i class="no-highlight">34</i>&nbsp;     * Loads a sprite from a resource on the class path.
<i class="no-highlight">35</i>&nbsp;     * Sprites are loaded once, and then stored in the store
<i class="no-highlight">36</i>&nbsp;     * so that they can be efficiently retrieved.
<i class="no-highlight">37</i>&nbsp;     *
<i class="no-highlight">38</i>&nbsp;     * @param resource
<i class="no-highlight">39</i>&nbsp;     *            The resource path.
<i class="no-highlight">40</i>&nbsp;     * @return The sprite for the resource.
<i class="no-highlight">41</i>&nbsp;     * @throws IOException
<i class="no-highlight">42</i>&nbsp;     *             When the resource could not be loaded.
<i class="no-highlight">43</i>&nbsp;     */
<i class="no-highlight">44</i>&nbsp;    public Sprite loadSprite(String resource) throws IOException {
<b class="nc"><i class="no-highlight">45</i>&nbsp;        Sprite result = spriteMap.get(resource);</b>
<b class="nc"><i class="no-highlight">46</i>&nbsp;        if (result == null) {</b>
<b class="nc"><i class="no-highlight">47</i>&nbsp;            result = loadSpriteFromResource(resource);</b>
<b class="nc"><i class="no-highlight">48</i>&nbsp;            spriteMap.put(resource, result);</b>
<i class="no-highlight">49</i>&nbsp;        }
<b class="nc"><i class="no-highlight">50</i>&nbsp;        return result;</b>
<i class="no-highlight">51</i>&nbsp;    }
<i class="no-highlight">52</i>&nbsp;
<i class="no-highlight">53</i>&nbsp;    /**
<i class="no-highlight">54</i>&nbsp;     * Loads a sprite from a resource on the class path.
<i class="no-highlight">55</i>&nbsp;     *
<i class="no-highlight">56</i>&nbsp;     * @param resource
<i class="no-highlight">57</i>&nbsp;     *            The resource path.
<i class="no-highlight">58</i>&nbsp;     * @return A new sprite for the resource.
<i class="no-highlight">59</i>&nbsp;     * @throws IOException
<i class="no-highlight">60</i>&nbsp;     *             When the resource could not be loaded.
<i class="no-highlight">61</i>&nbsp;     */
<i class="no-highlight">62</i>&nbsp;    @SuppressFBWarnings(
<i class="no-highlight">63</i>&nbsp;        value = &quot;RCN_REDUNDANT_NULLCHECK_OF_NONNULL_VALUE&quot;,
<i class="no-highlight">64</i>&nbsp;        justification = &quot;false positive in java 11&quot;
<i class="no-highlight">65</i>&nbsp;    )
<i class="no-highlight">66</i>&nbsp;    private Sprite loadSpriteFromResource(String resource) throws IOException {
<b class="nc"><i class="no-highlight">67</i>&nbsp;        try (InputStream input = SpriteStore.class.getResourceAsStream(resource)) {</b>
<b class="nc"><i class="no-highlight">68</i>&nbsp;            if (input == null) {</b>
<b class="nc"><i class="no-highlight">69</i>&nbsp;                throw new IOException(&quot;Unable to load &quot; + resource + &quot;, resource does not exist.&quot;);</b>
<i class="no-highlight">70</i>&nbsp;            }
<b class="nc"><i class="no-highlight">71</i>&nbsp;            BufferedImage image = ImageIO.read(input);</b>
<b class="nc"><i class="no-highlight">72</i>&nbsp;            return new ImageSprite(image);</b>
<b class="nc"><i class="no-highlight">73</i>&nbsp;        }</b>
<i class="no-highlight">74</i>&nbsp;    }
<i class="no-highlight">75</i>&nbsp;
<i class="no-highlight">76</i>&nbsp;    /**
<i class="no-highlight">77</i>&nbsp;     * Creates a new {@link AnimatedSprite} from a base image.
<i class="no-highlight">78</i>&nbsp;     *
<i class="no-highlight">79</i>&nbsp;     * @param baseImage
<i class="no-highlight">80</i>&nbsp;     *            The base image to convert into an animation.
<i class="no-highlight">81</i>&nbsp;     * @param frames
<i class="no-highlight">82</i>&nbsp;     *            The amount of frames of the animation.
<i class="no-highlight">83</i>&nbsp;     * @param delay
<i class="no-highlight">84</i>&nbsp;     *            The delay between frames.
<i class="no-highlight">85</i>&nbsp;     * @param loop
<i class="no-highlight">86</i>&nbsp;     *            Whether this sprite is a looping animation or not.
<i class="no-highlight">87</i>&nbsp;     * @return The animated sprite.
<i class="no-highlight">88</i>&nbsp;     */
<i class="no-highlight">89</i>&nbsp;    public AnimatedSprite createAnimatedSprite(Sprite baseImage, int frames,
<i class="no-highlight">90</i>&nbsp;                                               int delay, boolean loop) {
<b class="nc"><i class="no-highlight">91</i>&nbsp;        assert baseImage != null;</b>
<b class="nc"><i class="no-highlight">92</i>&nbsp;        assert frames &gt; 0;</b>
<i class="no-highlight">93</i>&nbsp;
<b class="nc"><i class="no-highlight">94</i>&nbsp;        int frameWidth = baseImage.getWidth() / frames;</b>
<i class="no-highlight">95</i>&nbsp;
<b class="nc"><i class="no-highlight">96</i>&nbsp;        Sprite[] animation = new Sprite[frames];</b>
<b class="nc"><i class="no-highlight">97</i>&nbsp;        for (int i = 0; i &lt; frames; i++) {</b>
<b class="nc"><i class="no-highlight">98</i>&nbsp;            animation[i] = baseImage.split(i * frameWidth, 0, frameWidth,</b>
<b class="nc"><i class="no-highlight">99</i>&nbsp;                baseImage.getHeight());</b>
<i class="no-highlight">100</i>&nbsp;        }
<i class="no-highlight">101</i>&nbsp;
<b class="nc"><i class="no-highlight">102</i>&nbsp;        return new AnimatedSprite(animation, delay, loop);</b>
<i class="no-highlight">103</i>&nbsp;    }
<i class="no-highlight">104</i>&nbsp;
<i class="no-highlight">105</i>&nbsp;}
</div>
</pre>
</div>

<script type="text/javascript">
(function() {
    var msie = false, msie9 = false;
    /*@cc_on
      msie = true;
      @if (@_jscript_version >= 9)
        msie9 = true;
      @end
    @*/

    if (!msie || msie && msie9) {
        var codeBlock = document.getElementById('sourceCode');

        if (codeBlock) {
            hljs.highlightBlock(codeBlock);
        }
    }
})();
</script>

<div class="footer">
    
    <div style="float:right;">generated on 2022-05-22 18:55</div>
</div>
</body>
</html>
